package com.atguigu.flink.day03;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.connector.file.src.FileSource;
import org.apache.flink.connector.file.src.reader.TextLineInputFormat;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/8/12
 * 该案例演示了从文件中读取数据
 */
public class Flink05_Source_File {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //TODO 2.从文件中读取数据
        //DataStreamSource<String> ds = env.readTextFile("D:\\dev\\workspace\\bigdata-0318\\input\\words.txt");
        FileSource<String> source = FileSource
                .forRecordStreamFormat(new TextLineInputFormat(), new Path("D:\\dev\\workspace\\bigdata-0318\\input\\words.txt"))
                .build();
        //封装为流(从指定的数据源读取数据  将其封装为流)
        //flink1.12前
        //env.addSource()
        DataStreamSource<String> ds = env.fromSource(source, WatermarkStrategy.noWatermarks(), "file_source");

        //TODO 3.打印输出
        ds.print();
        //TODO 4.提交作业
        env.execute();
    }
}
